package fr.mathis.ouestcharlie;

import com.enrique.stackblur.StackBlurManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class BitmapCacheManager {
    private static LruCache<Object, Bitmap> cache = null;
    private final Context context;
    private static final int KB = 1024;
    private final Drawable placeHolder;


    public BitmapCacheManager(Context context) {
        this.context = context;
        placeHolder = null;
        //placeHolder = context.getResources().getDrawable(R.drawable.ab_background);
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / KB);
        int cacheSize = maxMemory / 3;
        cache = new LruCache<Object, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(Object albumId, Bitmap bitmap) {
                return (bitmap.getRowBytes() * bitmap.getHeight() / KB);
            }

            protected void entryRemoved(boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) {
                oldValue.recycle();
            }
        };
    }

    public void addBitmapToMemoryCache(Object key, Bitmap bitmap) {
        if (bitmap != null && key != null && cache.get(key) == null)
            cache.put(key, bitmap);
    }

    public Bitmap getBitmapFromMemCache(Object key) {
        return cache.get(key);
    }

    public void loadBitmap(final Object key, final ImageView imageView) {
        final Bitmap bitmap = getBitmapFromMemCache(key);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else {
            imageView.setImageDrawable(placeHolder);
            BitmapWorker task = new BitmapWorker(imageView);
            task.execute(key);
        }
    }

    private class BitmapWorker extends AsyncTask<Object, Void, Bitmap> {
        private final ImageView imageView;
        private Object key;

        public BitmapWorker(final ImageView imageView) {
            this.imageView = imageView;
        }

        
        protected Bitmap doInBackground(Object... params) {
            key = params[0];
            final Bitmap b;;
            
    		if (((String)key).startsWith("1")) {
    			b = ((BitmapDrawable) context.getResources().getDrawable(Integer.parseInt(((String)key).substring(1)))).getBitmap();
    		} else {
    			StackBlurManager _stackBlurManager = new StackBlurManager(((BitmapDrawable) context.getResources().getDrawable(Integer.parseInt(((String)key).substring(1)))).getBitmap());
    			_stackBlurManager.process(20);
    			b = _stackBlurManager.returnBlurredImage();
    		}          
            
            addBitmapToMemoryCache(key, b);
            return b;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap == null) {
                imageView.setImageBitmap(null);
                return;
            }
            if (imageView.getTag().toString().equalsIgnoreCase(key.toString()) && !bitmap.isRecycled())
            {
				Animation anim = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
				imageView.startAnimation(anim);
                imageView.setImageBitmap(bitmap);
            }
        }
    }

}